package com.btcode.common;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XmlReader {

    Document doc = null;

    public XmlReader(String xmlContent)
        throws ParserConfigurationException, SAXException, IOException {

        ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlContent.getBytes());

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
        doc = dbBuilder.parse(inputStream);
    }

    public String getValue(String tagName) {
        NodeList nodeList = doc.getElementsByTagName(tagName);

        if (nodeList.getLength() > 0) {
            Node node = nodeList.item(0);
            String content = node.getTextContent();
            return content;
        }
        return null;
    }

}
